package com.omenkk.juc.synchronizedPlus;

public class GuardedObject {
    //任务id
private int id;
//任务内容
private Object response;

    public GuardedObject(int id) {
        this.id=id;
    }


    public int getId() {
        return id;
    }

    public Object getResponse() {
        return response;
    }

    public void setId(int id) {
        this.id = id;
    }

    public void setResponse(Object response) {
        this.response = response;
    }



    public Object get(long timeOut) throws InterruptedException {
synchronized (this){
    long begin = System.currentTimeMillis();
    long passTime=0;
    while(response==null){
        //可能中途被唤醒
long waitTime=timeOut-passTime;
if(waitTime<=0){
    break;
}
//如果等待超时了
       this.wait(waitTime);
        //求得经历时间
        passTime=System.currentTimeMillis()-begin;
    }
    return response;
}
    }

    public void complete(Object response){
        synchronized (this) {
            this.response = response;
            this.notifyAll();
        }

    }

}
